<?php
class LyDispatcher {
	protected $options = array();
	function setOptions($options){
		$this->options = array_merge($this->options,$options);
	}
	function dispatchAction($actionName,$params = array()){

		$newContext = new LyContext();
		if(!empty($params)){
			$newContext->addParams($params);
		}
		$className = $actionName.'Action';
		$this->dispatch($className,$newContext);
	}
	function dispatchComponent($compName,$context = null,$params = array()){
		if($context instanceof LyContext){
			$newContext = clone $context;
		}else{
			$newContext = new LyContext();
		}
		if(!empty($params)){
			$newContext->addParams($params);
		}
		$className = $compName.'Component';
		$this->dispatch($className,$newContext);
	}
	protected function dispatch($className,$newContext){
		$instance = new $className();
		$instance->setContext($newContext);
		if(method_exists($instance,'init')){
			$instance->init();
		}
		$instance->run();
	}
}